<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input v-model="params.username" style="width: 200px" placeholder="请输入用户名"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 10px" @click="findPageByUserName"></el-button>
      <el-button type="warning" icon="el-icon-refresh-right" style="margin-left: 10px" @click="reset"></el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-left: 10px" @click="addUser">新增</el-button>
    </div>
    <!--用户信息表-->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="nickname" label="昵称"></el-table-column>
        <el-table-column prop="userType" label="用户类型"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row)"></el-button>
            <el-popconfirm title="确定删除吗？" @confirm="deleteUser(scope.row.id)">
              <el-button slot="reference" type="danger" icon="el-icon-delete" style="margin-left: 5px"></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页查询-->
    <div style="margin-top: 10px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <!--新增&编辑用户表单-->
    <div>
      <el-dialog title="请填写用户信息" :visible.sync="dialogFormVisible" width="40%">
        <el-form :model="form" style="display: flex">
          <el-form-item label="用户名">
            <el-input v-model="form.username" :disabled="!addOrUpdate" autocomplete="off" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickname" autocomplete="off" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" :disabled="!addOrUpdate" autocomplete="off" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="用户类型">
            <el-input v-model="form.userType" :disabled="!addOrUpdate" autocomplete="off" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitUserInfo">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>


import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        username: '',
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      addOrUpdate: false,
      form: {}
    }
  },
  created() {
    // this.load();
    this.findPageByUserName()
  },
  methods: {
    // load() {
    //   request.get("/user").then(res => {
    //     if (res.code === '0') {
    //       this.tableData = res.data;
    //     }
    //   })
    // },
    //按用户名查询
    findPageByUserName() {
      request.get("/user/search/page", {
        params: this.params
      }).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    //清空
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 5,
        username: ''
      }
      this.findPageByUserName()
    },
    //改变每页显示条数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findPageByUserName();
    },
    //改变当前在第几页
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findPageByUserName();
    },
    addUser() {
      this.dialogFormVisible = true;
      this.addOrUpdate = true;
      this.form = {};
    },
    editUser(obj) {
      this.form = obj;
      this.dialogFormVisible = true;
      this.addOrUpdate = false;
    },
    deleteUser(id) {
      request.delete("/user/delete/" + id).then(res => {
        if (res.code === "0") {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findPageByUserName();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    submitUserInfo() {
      request.post("/user/save",this.form).then(res => {
        if (res.code === "0") {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findPageByUserName();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })

    }
  }
}
</script>
